package com.oa.dao.impl;

import com.oa.common.base.Page;
import com.oa.common.base.Pageable;
import com.oa.common.eums.EntityStatus;
import com.oa.common.interfaces.PreFilter;
import com.oa.dao.BaseDaoImpl;
import com.oa.dao.PreFilters;
import com.oa.dao.RoleDao;
import com.oa.entities.Role;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Repository
public class RoleDaoImpl extends BaseDaoImpl<Long,Role> implements RoleDao {

    @Override
    protected void loadClassType() {
        setType(Role.class);
    }

    @Override
    public Page<Role> findPage(Pageable pageable, String key) {
        List<PreFilter> filters= new ArrayList<>();
        if (!StringUtils.isEmpty(key)){
            filters.add(PreFilters.like(Role.ROLE_NAME,"%"+key+"%"));
        }
        filters.add(PreFilters.eq(Role.PROP_STATUS,EntityStatus.ENTITY_EABLE.getCode()));
        pageable.setFilters(filters);
        return findPage(pageable);
    }

    @Override
    public Boolean validate(String name,Long id) {
        List<PreFilter> filters= new ArrayList<>();
        filters.add(PreFilters.eq(Role.ROLE_NAME,name));
        filters.add(PreFilters.eq(Role.PROP_STATUS,EntityStatus.ENTITY_EABLE.getCode()));
        if (id!=null){
            filters.add(PreFilters.ne(Role.PROP_ID,id));
        }
        PreFilter[] fs= new PreFilter[filters.size()];
        return findUnqiue(filters.toArray(fs))!=null;
    }
}
